package org.springblade.modules.enterprise.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.controller.CompanyBasicInfController;
import org.springblade.modules.enterprise.controller.ContentController;
import org.springblade.modules.enterprise.controller.VisitorCountController;
import org.springblade.modules.enterprise.entity.VisitorCount;
import org.springblade.modules.enterprise.vo.FrontIndexVO;
import org.springblade.modules.enterprise.vo.VisitorCountVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@Slf4j
@AllArgsConstructor
@Api(value = "首页信息", tags = "(api)首页信息")
@RestController
@RequestMapping("/enterprise/index-info")
public class IndexApi {

	CompanyBasicInfController companyBasicInfController;

	ContentController contentController;

	VisitorCountController visitorCountController;

	@GetMapping
	@ApiOperation(value = "获取首页统计信息")
	public R<FrontIndexVO> getInfo() {
		final FrontIndexVO indexVO = new FrontIndexVO();
		// 1、所有企业浏览政策总数
		Integer count = companyBasicInfController.getPolicyLookCount().getData();
		indexVO.setPolicyLookCount(count);
		// 2、政策文章总数
		final Integer policyContentCount = contentController.getPolicyContentCount().getData();
		indexVO.setPolicyCount(policyContentCount);
		// 3、总访问量
		final Long lookCount = visitorCountController.getLookCount().getData();
		indexVO.setLookCount(lookCount);
		// 4、今日访问量
		try {
			final VisitorCount visitorCount = new VisitorCount();
			visitorCount.setCurrDay(LocalDate.now());
			final R<VisitorCountVO> visitorCountVO = visitorCountController.detail(visitorCount);
			if (Func.isNotEmpty(visitorCountVO)) {
				final VisitorCountVO visitorCountVOData = visitorCountVO.getData();
				indexVO.setTodayLookCount(visitorCountVOData.getCount());
			}
		} catch (Exception e) {
			log.error("今日访问量为空");
		}
		// 5、用户量
		final Integer companyCount = companyBasicInfController.count().getData();
		indexVO.setUserCount(companyCount);
		return R.data(indexVO);
	}

}
